package com.zxj.hadoop.demo.mapreduce.findfriend;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author 朱小杰
 * 时间 2017-08-24 .22:59
 * 说明 继续第第二步操作
 */
public class Friend2 {


    static class FriendMapper2 extends Mapper<LongWritable, Text, Text, Text> {
        /**
         * 这里拿到的是上一次计算的数据  A	I,K,C,B,G,F,H,O,D,
         * A是哪些用户的好友
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] split = line.split("\t");
            String friend = split[0];
            String[] persions = split[1].split(",");
            Arrays.sort(persions);

            for(int i = 0 ; i < persions.length -2 ; i ++){
                for(int j = i+1 ; j < persions.length -1 ; j ++){
                    //发送出 人-人  好友的数据，就是这两个人有哪个共同好友，会进入到同一个reducer中
                    context.write(new Text(persions[i] + "-" + persions[j]),new Text(friend));
                }
            }
        }
    }

    /**
     * 输入 好友，用户
     */
    static class FriendReduce2 extends Reducer<Text,Text,Text,Text>{
        private Text k = new Text();
        private Text v = new Text();
        @Override
        protected void reduce(Text person_person, Iterable<Text> friends, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for(Text f : friends){
                sb.append(f.toString()).append(" ");
            }
            context.write(person_person,new Text(sb.toString()));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        String input = "D:\\mr\\qq\\out1";
        String output = "D:\\mr\\qq\\out2";
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(Friend2.class);

        job.setMapperClass(FriendMapper2.class);
        job.setReducerClass(FriendReduce2.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job,new Path(input));
        FileOutputFormat.setOutputPath(job,new Path(output));

        boolean b = job.waitForCompletion(true);
        if(b){}

    }
}
